package com.yunxitest.yunxitest.utils;

import com.yunxitest.yunxitest.AppApplication;
import com.yunxitest.yunxitest.item.info.Info;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import static com.yunxitest.yunxitest.utils.Common.getNowTime2;


/**
 * 信息日志.
 */
public final class TestLog {

    /**
     * 工具类私有构造器.
     */
    private TestLog() {

    }

    /**
     * 最大文件大小.
     */
    private static final long MAX_SIZE = 2 * 1024 * 1024;

    /**
     * 文件.
     */
    private static RandomAccessFile file;
    
    static String TEST_LOG="test.log";
    static String TEST_LOG1="test1.log";


    /**
     * 打印日志.
     *
     * @param msg 日志信息
     */
    public static synchronized void writeLog(final String msg) {

        if (LogConfig.LOG_LEVEL > LogConfig.INFO) {
            File pt = new File(LogConfig.PATH);
            if (!pt.exists()) {
                pt.mkdirs();
            }
            try {
                if (file == null) {
                    file = new RandomAccessFile(LogConfig.PATH
                            + Info.getDeviceId(AppApplication.Companion.instance())+"-"+getNowTime2(), "rw");
                }
                if (file.length() > MAX_SIZE) {
                    FileUtils.fileCopy(LogConfig.PATH + TEST_LOG,
                            LogConfig.PATH + TEST_LOG1);
                    file.close();
                    File f = new File(LogConfig.PATH + TEST_LOG);
                    f.delete();
                    file = new RandomAccessFile(LogConfig.PATH
                            + TEST_LOG, "rw");
                }
                file.seek(file.length());
                String curTime = Common.getNowTime();
//                curTime + " :    " + Common.generateTag() +
                file.write(("" + msg + "\r\n").getBytes());
            } catch (FileNotFoundException e) {
                file = null;
            } catch (IOException e) {
                file = null;
            }
        }
    }




}
